package com.next

import static org.springframework.http.HttpStatus.*
import grails.gorm.transactions.Transactional
import org.springframework.security.access.annotation.Secured

@Secured(['ROLE_ADMINISTRATOR'])
@Transactional(readOnly = true)
class RoleMenuController
{

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def index(Integer max)
    {
        params.max = Math.min(max ?: 10, 100)
        def menuid = params["menu"]
        respond RoleMenu.findAll("from RoleMenu rm where rm.menu.id = " + menuid), model: [roleMenuCount: RoleMenu.count(), menu: menuid]
    }

    def show(RoleMenu roleMenu)
    {
        def menuid = params["menu"]
        respond roleMenu, model: [menu: menuid]
    }

    def create()
    {
        RoleMenu m = new RoleMenu(params)
        m.menu = Menu.findById(params["menu"])
        respond m
    }

    @Transactional
    def save(RoleMenu roleMenu)
    {
        if (roleMenu == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (roleMenu.hasErrors())
        {
            transactionStatus.setRollbackOnly()
            respond roleMenu.errors, view: 'create'
            return
        }

        def menuid = params["menu"]
        Menu menu = Menu.findById(menuid)
        roleMenu.menu = menu

        roleMenu.save flush: true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.created.message', args: [message(code: 'roleMenu.label', default: 'RoleMenu'), roleMenu.id])
                //redirect roleMenu
                redirect(action: "index", params: [menu: menuid], method: "GET")
            }
            '*' { respond roleMenu, [status: CREATED] }
        }
    }

    def edit(RoleMenu roleMenu)
    {
        respond roleMenu
    }

    @Transactional
    def update(RoleMenu roleMenu)
    {
        if (roleMenu == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (roleMenu.hasErrors())
        {
            transactionStatus.setRollbackOnly()
            respond roleMenu.errors, view: 'edit'
            return
        }

        def menuid = params["menu"]
        Menu menu = Menu.findById(menuid)
        roleMenu.menu = menu
        roleMenu.save flush: true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'roleMenu.label', default: 'RoleMenu'), roleMenu.id])
                //redirect roleMenu
                redirect(action: "index", params: [menu: menuid], method: "GET")
            }
            '*' { respond roleMenu, [status: OK] }
        }
    }

    @Transactional
    def delete(RoleMenu roleMenu)
    {

        if (roleMenu == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        def menuid = params["menu"]
        Menu menu = Menu.findById(menuid)
        roleMenu.menu = menu
        roleMenu.delete flush: true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'roleMenu.label', default: 'RoleMenu'), roleMenu.id])
                //redirect action:"index", method:"GET"
                redirect(action: "index", params: [menu: menuid], method: "GET")
            }
            '*' { render status: NO_CONTENT }
        }
    }

    protected void notFound()
    {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'roleMenu.label', default: 'RoleMenu'), params.id])
                redirect action: "index", method: "GET"
            }
            '*' { render status: NOT_FOUND }
        }
    }
}
